import { webapp as format } from '../format/index'
import { isApple } from '../utils/platform'

/**
 * @member H5_format
 * @description 格式化webapp数据
 * @description 该插件将为stat实例提供 setClientDetail 方法，该方法用于设置：渠道，平台，用户id
 * @param {String} channel 渠道
 * @param {String} platform 平台
 * @param {String} userId 用户id
 */
export default class H5_format {
	constructor({ channel = '', platform = '', userId = '', ...options } = {}) {

		this.options = {
			channel,
			platform: platform || (isApple() ? 'ios' : 'android'),
			userId,
			...options,
		}
	}

	apply = stat => {
		console.log('Stat use: H5_format')

		// 设置渠道，平台，用户id
		stat.setClientDetail = options => Object.assign(this.options, options)

		stat.pipe(datas => [format(this.options, datas)])
	}
}